package net.zjitc.servlet.product;

import net.zjitc.domain.Product;
import net.zjitc.domain.User;
import net.zjitc.service.product.ProductService;
import net.zjitc.service.product.productServiceImpl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/19.
 */
@WebServlet(name = "AddCartServlet",urlPatterns = "/addCartServlet")
public class AddCartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            String msg = "请先登陆";
            request.getSession().setAttribute("loginMsg",msg);
            response.sendRedirect("/newweb/log/login.jsp");
        }else {
            String id = request.getParameter("pid");
            ProductService service = new ProductServiceImpl();
            Product product = service.findById(id);
            List cart = (List) request.getSession().getAttribute("cart");
            if (cart==null){
                List list = new ArrayList();
                request.getSession().setAttribute("cart",list);
                cart = (List) request.getSession().getAttribute("cart");
            }
            cart.add(product);

            request.getSession().setAttribute("cart",cart);
            response.sendRedirect("/newweb/cart/cartlist.jsp");
        }
    }
}
